package manytoone.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import manytoone.pojo.Student;
import manytoone.service.StudentService;
import manytoone.service.impl.StudentServiceImpl;
import manytoone.util.ThUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.web.IWebExchange;
import org.thymeleaf.web.servlet.JakartaServletWebApplication;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;

/**
 * @Author 宋伟宁
 * @Date 2023/9/6
 * @Version 1.0
 **/
@WebServlet(urlPatterns = {"/list","/toadd","/save","/delete","/find"})
public class StudentServlet  extends HttpServlet {

    private StudentService studentService = new StudentServiceImpl();


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        System.out.println("请求路径是："+servletPath);

        IWebExchange webExchange = JakartaServletWebApplication.buildApplication(getServletContext())
                .buildExchange(req, resp);
        WebContext webContext = new WebContext(webExchange);

        PrintWriter out = resp.getWriter();
        //创建一个日期转换器
        DateConverter dateConverter = new DateConverter();
        //设置格式
        dateConverter.setPatterns(new String[]{"yyyy-MM-dd"});
        //注册格式
        ConvertUtils.register(dateConverter, Date.class);

        switch (servletPath){
            case "/find"->{
                String sid = req.getParameter("id");
                Student student= this.studentService.findById(sid);
                webContext.setVariable("student",student);
                webContext.setVariable("rs",this.studentService.findRooms());
                ThUtils.print("/view/update.html",webContext,resp);
            }
            case "/delete"->{
                String ids = req.getParameter("ids");
                out.print(com.alibaba.fastjson2.JSON.toJSONString(this.studentService.delete(ids)));
                out.close();
            }
            case "/save" ->{
                Map<String, String[]> map = req.getParameterMap();
                Student student = new Student();
                try {
                    BeanUtils.populate(student,map);
                    if(this.studentService.save(student)){
                        resp.sendRedirect("list");
                    }
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(e);
                }


            }
            case "/toadd"->{
                //传递班级集合
                webContext.setVariable("rs",this.studentService.findRooms());
                ThUtils.print("/view/add.html",webContext,resp);
            }
            case "/list" ->{
                String offset = req.getParameter("offset");
                offset = offset == null ? "1": offset;
                //接收模糊
                String mohu = req.getParameter("mohu");
                webContext.setVariable("pager",this.studentService.findAll(offset,mohu));
                webContext.setVariable("mohu",mohu);
                ThUtils.print("/view/list.html",webContext,resp);
            }
        }
    }
}
